define([], function() {
	return {
		title: ['<a href="user.html">个人中心</a>', "系统管理", "用户管理"],
		init: function(content) {
			let form = content.children("form");
			let table = content.children("table");
			let search = [];
			let roleId = content.find("select[name='roleId']");
			let roleTemplate = content.find("._roleTemplate");
			let tableInit = function() {
				let actionEvent = {
					"click .temp2": function(e, value, row) {
						toastr.info("查看详情[" + row.userRealName + "]开发中...");
					},
					"click .temp1": function(e, value, row) {
						$.post(
							"user/modify-pwd.do", {
								"userName": row.userName,
								"userPwd": "123456"
							},
							function(data) {
								if(data.ok) {
									toastr.success("重置密码为:123456");
								} else {
									toastr.error("设置锁定状态失败：" + data.message);
								}
							}, "json"
						);
					},
					"change .userLocked": function(e, value, row) {
						e.target.disabled = true;
						$.post(
							"user/modify.do", {
								"userName": row.userName,
								"userLocked": e.target.checked
							},
							function(data) {
								e.target.disabled = false;
								if(data.ok) {
									toastr.success("设置锁定状态成功");
								} else {
									toastr.error("设置锁定状态失败：" + data.message);
								}
							}, "json"
						);
					},
					"change .roleId": function(e, value, row) {
						e.target.disabled = true;
						$.post(
							"user/modify.do", {
								"userName": row.userName,
								"roleId": e.target.value
							},
							function(data) {
								e.target.disabled = false;
								if(data.ok) {
									toastr.success("设置角色成功");
								} else {
									toastr.error("设置角色失败：" + data.message);
								}
							}, "json"
						);
					},
					"click .temp": function(e, value, row) {}
				};
				table.bootstrapTable({
					url: 'user/page.do', // 表格数据来源
					method: "get",
					sidePagination: "server", //分页方式：client客户端分页，server服务端分页（*）
					pageNumber: 1, //初始化加载第一页，默认第一页
					pageSize: 10, //每页的记录行数（*）
					pageList: [5, 10, 15, 20, 30, 50], //可供选择的每页的行数（*）
					pagination: true, //是否显示分页（*）
					dataField: "list",
					toolbar: form,
					queryParamsType: "",
					queryParams: function(p) {
						let ps = [].concat(search);
						ps.push({
							"name": "current",
							"value": p.pageNumber
						});
						ps.push({
							"name": "rowMax",
							"value": p.pageSize
						});
						ps.push({
							"name": "sortName",
							"value": p.sortName
						});
						ps.push({
							"name": "sortOrder",
							"value": p.sortOrder
						});
						return ps;
					},
					columns: [{
							field: 'userName',
							title: '用户名',
							sortable: true,
							sortName: "USER_NAME",
							width: 100
						}, {
							field: 'userRealName',
							title: '昵称',
							sortable: true,
							sortName: "USER_REAL_NAME",
							width: 100
						}, {
							field: 'userSex',
							title: '性别',
							align: "center",
							width: 40,
							formatter: function(value, row, index, field) {
								if(value == true) {
									return "男";
								} else if(value == false) {
									return "女"
								} else {
									return "-";
								}
							}
						}, {
							field: 'userBirthdate',
							sortable: true,
							sortName: "USER_BIRTHDATE",
							align: "center",
							width: 100,
							title: '生日'
						}, {
							field: 'userTel',
							sortable: true,
							sortName: "USER_TEL",
							align: "center",
							width: 100,
							title: '手机号码'
						}, {
							field: 'userLocked',
							align: "center",
							title: '锁定',
							width: 40,
							events: actionEvent,
							formatter: function(value, row, index, field) {
								if(value) {
									return "<input class='userLocked' type='checkbox' checked='checked' />";
								}
								return "<input class='userLocked' type='checkbox' />";
							}
						}, {
							field: 'roleId',
							align: "center",
							title: '角色',
							width: 120,
							events: actionEvent,
							formatter: function(value, row, index, field) {
								let s = roleTemplate.children("select");
								s.children().removeAttr("selected").filter("[value='" + value + "']").attr("selected", "selected");
								return roleTemplate.html();
							}
						}, {
							field: 'temp1',
							align: "center",
							title: '重置密码',
							width: 100,
							events: actionEvent,
							formatter: function(value, row, index, field) {
								return '<a class="temp1" href="javascript:void(0);">重置密码</a>';
							}
						}, {
							field: 'temp2',
							align: "center",
							title: '查看详情',
							width: 100,
							events: actionEvent,
							formatter: function(value, row, index, field) {
								return '<a class="temp2" href="javascript:void(0);">查看详情</a>';
							}
						}

					]
					
				});
			};
			form.on("submit", function() {
				search = form.serializeArray();
				table.bootstrapTable('refresh');
				return false;
			});
			$.get(
				"role/list.do",
				function(data) {
					$.each(data, function(i, v) {
						$("<option></option>")
							.val(v.roleId).text(v.roleName)
							.appendTo(roleId);
					});
					tableInit();
				}, "json"
			);
		}
	}
});